package Day01;

/**
 * 二分查找
 */
public class BinarySearch {


    // 左边界
    public static int binarySearch1(int[] arr, int l, int r, int tag) {
        while (l < r) {
            int mid = (l + r) / 2;
            if (arr[mid] >= tag) {
                r = mid;
            } else {
                l = mid + 1;
            }
        }
        return l;
    }

    // 右边界
    public static int binarySearch2(int[] arr, int l, int r, int x) {
        while (l < r) {
            int mid = (l + r + 1) / 2;
            if (arr[mid] <= x) {
                l = mid;
            } else {
                r = mid - 1;
            }
        }
        return l;
    }

    // 浮点数二分
    private static double EPS = 1e-6;

    private static double floatBinarySearch(double left, double right, double x) {
        while (right - left > EPS) {
            double mid = (left + right) / 2;
            if (mid * mid >= x){
                right = mid;
            } else{
                left = mid;
            }
        }
        return left;
    }


    public static void main(String[] args) {
//        int[] arr = new int[]{1, 2, 2, 3};
//        int[] arr2 = new int[]{12, 12, 44, 55, 66, 77, 88, 88, 99};
//        int a = binarySearch1(arr2, 0, arr2.length, 88);
//        int b = binarySearch2(arr2, 0, arr2.length, 88);
//        System.out.println(a + "," + b);
        System.out.println(floatBinarySearch(0,Double.MAX_VALUE,4));
    }


}
